using System.Collections.Generic;
using Dream.FirstLevel.Base;
using UnityEngine;
using UnityEngine.UI;

namespace Dream.FirstLevel
{
    public class ProductList : MonoBehaviour
    {
        [SerializeField] private GameObject productListUI;

        [SerializeField] private GameObject productUIPrefab;

        [SerializeField] private string tempName;
        public List<ProductBase> productsList = new();
        private List<GameObject> productUIList;

        private void Awake()
        {
            productListUI = transform.Find("Canvas/Background").gameObject;
        }

        private void Start()
        {
            productUIList = new List<GameObject>();
            var go = new GameObject();
            go.name = tempName;
            for (var i = 0; i < 3; i++) productUIList.Add(go);
        }

        /// <summary>
        ///     一次性初始化
        /// </summary>
        /// <param name="list"></param>
        public void InitProductList(List<ProductBase> list)
        {
            productsList = list;
            foreach (var productBase in productsList) SwitchProduct(productBase, 1);
        }

        public void AddProduct(ProductBase product)
        {
            productsList.Add(product);
            SwitchProduct(product, 1);
        }

        public void RemoveProduct(ProductBase product)
        {
            productsList.Remove(product);
            SwitchProduct(product, -1);
        }

        private async void SwitchProduct(ProductBase product, int operation)
        {
            var sprite = await product.sprite();
            switch (product.GetType().Name)
            {
                case "ProductA":
                    CheckProduct(0, sprite, operation);
                    break;
                case "ProductB":
                    CheckProduct(1, sprite, operation);
                    break;
                case "ProductC":
                    CheckProduct(2, sprite, operation);
                    break;
            }
        }

        //mao 24-11-23 08:12
        //复合操作，有增有减
        private void CheckProduct(int index, Sprite sprite, int operation)
        {
            if (productUIList[index].name == tempName)
            {
                if (operation == -1) return; //如果操作是remove
                var go = Instantiate(productUIPrefab, productListUI.transform);
                go.GetComponentInChildren<Image>().sprite = sprite;
                go.GetComponentInChildren<Text>().text = "1";
                productUIList[index] = go;
            }
            else
            {
                var text = productUIList[index].GetComponentInChildren<Text>();
                var num = int.Parse(text.text);

                //当现有值为1且操作是减去的时候
                if (operation == -1)
                {
                    if (num == 1)
                    {
                        //销毁TEXT的父
                        Destroy(text.transform.parent.gameObject);
                        GameObject go = new();
                        go.name = tempName;
                        productUIList[index] = go;
                        return;
                    }

                    num -= 1;
                    text.text = num.ToString();
                    return;
                }

                num += 1;
                text.text = num.ToString();
            }
        }
    }
}